package com.chinatgl.latte.util.storage;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.chinatgl.latte.app.Latte;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


public class SpUtil {

    private static final SharedPreferences PREFERENCES= PreferenceManager.getDefaultSharedPreferences(Latte.getApplication());

    private static SharedPreferences getPreferences()
    {
    return PREFERENCES;
    }
    /**
     * 存储List<String>
     * @param key     List<String>对应的key
     * @param strList 对应需要存储的List<String>
     */
    public static void putStrListValue(String key,
                                       List<String> strList) {
        if (null == strList) {
            return;
        }
        // 保存之前先清理已经存在的数据，保证数据的唯一性
        removeStrList(key);

        List<String> list = removeDuplicateWithOrder(strList);

        int size = list.size();
        setInt(key + "size", size);
        for (int i = 0; i < size; i++) {
            setString(key + i, list.get(i));
        }
    }

    private static List removeDuplicateWithOrder(List list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            Object element = iter.next();
            if (set.add(element))
                newList.add(element);
        }
        list.clear();
        list.addAll(newList);
        return list;
    }

    /**
     * 取出List<String>
     *
     * @param key List<String> 对应的key
     * @return List<String>
     */
    public static List<String> getStrListValue(String key) {
        List<String> strList = new ArrayList<String>();
        int size = getInt(key + "size");
        for (int i = 0; i < size; i++) {
            strList.add(getString(key + i));
        }
        return strList;
    }

    /**
     * 清空List<String>所有数据
     * @param key List<String>对应的key
     */
    public static void removeStrList(String key) {
        int size = getInt(key + "size");
        if (0 == size) {
            return;
        }
        remove(key + "size");
        for (int i = 0; i < size; i++) {
            remove(key + i);
        }
    }


    /**
     * 获取布尔值
     *
     * @param value 传入记录的变量名
     * @return 返回保存的具体的boolean值
     */
    public static boolean getBoolean(String value) {
        return getPreferences()
                .getBoolean(value, false);
    }

    /**
     * 获取保存的字符串
     * @param key
     * @return
     */
    public static String getString(String key) {
        return getPreferences()
                .getString(key, null);
    }

    /**
     * 获取保存的int类型的值
     * @return
     */
    public static int getInt(String key) {
        return getPreferences()
                .getInt(key, -1);
    }

    /**
     * 存储int值
     *
     * @param key
     * @param value
     */
    public static void setInt(String key, int value) {
        getPreferences()
                .edit().putInt(key, value).commit();
    }

    /**
     * 存储boolean值
     *
     * @param key
     * @param value
     */
    public static void setBoolean(String key, boolean value) {
        getPreferences()
                .edit().putBoolean(key, value).commit();
    }

    /**
     * 存储string值
     *
     * @param key
     * @param value
     */
    public static void setString(String key, String value) {
        getPreferences()  .edit().putString(key, value).commit();
    }

    /***
     * 清空sp存储根据制定的key
     */
    public static void remove(String key) {
        getPreferences()
                .edit().remove(key).commit();
        //putString(key, value).commit();
    }

    /***
     * 清空sp所有的存储
     */
    public static void clear() {
        getPreferences()
                .edit().clear().commit();
        //putString(key, value).commit();
    }

}